package com.hangwei.paperless.adapter.accept;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.net.API;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.ConformDialog;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;


public abstract class Item3Adapter extends BaseItemDraggableAdapter<AcceptCheckInfo.TBean, BaseViewHolder> {

    private boolean mEnable = true;
//    private int index = -1;


    public Item3Adapter(List<AcceptCheckInfo.TBean> data) {
        super(R.layout.item_page_view3, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final AcceptCheckInfo.TBean item) {
        helper.setIsRecyclable(false);
        helper.setText(R.id.tvSeq, Tools.checkedString(item.seq))
                .setText(R.id.etDes, Tools.checkedString(item.description))
                .setText(R.id.etPN, Tools.checkedString(item.pn))
                .setText(R.id.etSN, Tools.checkedString(item.sn))
                .setText(R.id.etRemark, Tools.checkedString(item.remark))
                .setOnClickListener(R.id.ivScan, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(item.id)){
                            ToastUtil.showCenterToast("请先保存！");
                            return;
                        }
                        scan(item.id);
                    }
                });
        EditText etRemark = helper.getView(R.id.etRemark);
        EditText etSN = helper.getView(R.id.etSN);
        EditText etPN = helper.getView(R.id.etPN);
        EditText etDes = helper.getView(R.id.etDes);
//        helper.addOnClickListener(R.id.ivScan);
        if (mEnable) {
            etDes.setEnabled(item.isAdd);
            etDes.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.description = s.toString().trim();
                }
            });
            etRemark.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.remark = s.toString().trim();
                }
            });
            etSN.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.sn = s.toString().trim();
                }
            });
            etPN.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.pn = s.toString().trim();
                }
            });
        } else {
            etDes.setEnabled(false);
            etRemark.setEnabled(false);
            etSN.setEnabled(false);
            etPN.setEnabled(false);
        }
        RecyclerView recyclerViewPic = helper.getView(R.id.recyclerViewPic);
        if (item.picList != null && item.picList.size() > 0) {
            helper.setGone(R.id.llPic, true);
            LinearLayoutManager layout = new LinearLayoutManager(mContext);
            layout.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerViewPic.setLayoutManager(layout);
            recyclerViewPic.setAdapter(new PicAdapter(item.picList));
        } else {
            helper.setGone(R.id.llPic, false);
        }
    }

    protected abstract void scan(String id);

    @Override
    public void onItemSwiped(final RecyclerView.ViewHolder viewHolder) {
        final int position = getViewHolderPosition(viewHolder);
        if (mData.get(position).isAdd)
            new ConformDialog(mContext, "删除这条数据？") {
                @Override
                protected void onConfirm() {
                    if (TextUtils.isEmpty(mData.get(position).id))
                        Item3Adapter.super.onItemSwiped(viewHolder);
                    else {//已经保存了
                        final LoadingDialog loadingDialog = new LoadingDialog(mContext);
                        loadingDialog.show();
                        String userId = SPUtil.getString(SPKey.Account.USER_ID);
                        StaticPresenter.removeReceiveRecordSheetRow(userId, mData.get(position).id, new CallBack<Object>() {
                            @Override
                            public void onCompleted(Object o) {
                                loadingDialog.dismiss();
                                Item3Adapter.super.onItemSwiped(viewHolder);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError() {
                                loadingDialog.dismiss();
                                ToastUtil.showCenterToast("删除失败");
                            }
                        });
                    }
                }

                @Override
                protected void onCancel() {
                    notifyDataSetChanged();
                }
            };
        else {
            ToastUtil.showCenterToast("原始数据不能删除！");
            notifyDataSetChanged();
        }
    }

    public void setEnable(boolean enable) {
        mEnable = enable;
    }

   private class PicAdapter extends RecyclerView.Adapter {

        private ArrayList<String> mPicList;

        public PicAdapter(ArrayList<String> picList) {
            mPicList = picList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_item_sub_data, viewGroup, false)) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            SimpleDraweeView sdv = viewHolder.itemView.findViewById(R.id.sdv);
            sdv.setImageURI(API.BASE_URL + mPicList.get(i));
            final int position = i;
            sdv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showBigPic(mPicList, position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mPicList == null ? 0 : mPicList.size();
        }
    }

    protected abstract void showBigPic(ArrayList<String> picList, int position);

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onCameraEvent(CameraEvent event) {
//        if (index != -1) {
//            getData().get(index).picList.add(event.getPicturePath());
//            notifyDataSetChanged();
//        }
//    }
}
